//模版
import api from '../api/index.ts';
export const DownLoadTemp = (
  form: object = {
    templateTypeCode: '', //	模板类型编号
    customerCode: '', //	客户编号
    condition: {}, //查询条件
  },
  printType: any = 1,
  dataInfo?: any,
  fun?: Function,
) => {
  switch (printType) {
    case 1: //打印
      setLoading(dataInfo, '正在准备打印,请稍等...');
      api.Public.DownLoadTemplate(form)
        .then((res: any) => {
          closeLoading(dataInfo, res);
          if (typeof fun === 'function') {
            fun(res);
          }
        })
        .catch((e) => {
          closeLoading(dataInfo);
        });
      break;
    case 3: //下载Excel
      setLoading(dataInfo, '正在准备下载,请稍等...');
      api.Public.DownLoadTemplateExcel(form)
        .then((res: any) => {
          closeLoading(dataInfo, res);
          if (typeof fun === 'function') {
            fun(res);
          }
        })
        .catch((e) => {
          closeLoading(dataInfo);
        });
      break;
    default:
      break;
  }
};
const setLoading = (dataInfo: any, title: string = '正在获取数据,请稍等...') => {
  if (dataInfo && !dataInfo.loading) {
    dataInfo.loading = true;
    dataInfo.loadingMsg = title;
  }
};
const closeLoading = (dataInfo?: any, res?: any) => {
  if (dataInfo) {
    dataInfo.loading = false;
  }
  if (res) {
    window.open(res, '_blank');
  }
};
